package com.taomu.app.zongchouhuli.moudle.mine.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.res.ResProductRebateEntity;
import com.taomu.app.zongchouhuli.moudle.mine.entity.RebateDetailEntity;
import com.taomu.app.zongchouhuli.moudle.mine.entity.RebateListEntity;
import com.taomu.app.zongchouhuli.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class RebateDetailAdp extends RecyclerView.Adapter<RebateDetailAdp.RebateDetailHolder>{

    private List<ResProductRebateEntity.ListBean> mData = new ArrayList<>();

    private Context mContext;

    private LayoutInflater inflater;

    private String productName;

    public RebateDetailAdp(Context mContext){
        this.mData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
    }

    public void setProductName(String productName){
        this.productName = productName;
    }

    public void setData(List<ResProductRebateEntity.ListBean> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void addData(List<ResProductRebateEntity.ListBean> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public RebateDetailHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_rebate_detail,parent,false);
        return new RebateDetailHolder(view);
    }

    @Override
    public void onBindViewHolder(RebateDetailHolder holder, int position) {
        final ResProductRebateEntity.ListBean entity = mData.get(position);
        holder.update(entity);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class RebateDetailHolder extends RecyclerView.ViewHolder{

        private TextView mTitleTv;

        private TextView mPriceTv;

        private TextView mTimeTv;

        public RebateDetailHolder(View itemView) {
            super(itemView);
            mTitleTv = itemView.findViewById(R.id.item_rebate_detail_title_tv);
            mPriceTv = itemView.findViewById(R.id.item_rebate_detail_price_tv);
            mTimeTv = itemView.findViewById(R.id.item_rebate_detail_time_tv);
        }

        public void update(ResProductRebateEntity.ListBean entity){
            if(!TextUtils.isEmpty(entity.getCreate_date())) {
                mTimeTv.setText(TimeUtils.timeFomart(Long.valueOf(entity.getCreate_date())));
            }
            mPriceTv.setText(entity.getAccount());
            if(!TextUtils.isEmpty(productName)) {
                mTitleTv.setText(productName);
            }
        }
    }
}
